<?php
/**
 * User Groups List
 * Display All available user group
 * 
 * @author Xavier VILAIN
 * @package Hive\User\Group
 * @license #license-hive#
 */

use Fox\Ddic\DataElement\Formater\Void;
use Fox\Grid\FieldCat\Field\Renderer\Token;
use Fox\Behaviors;
use Fox\Html\Button;
use Fox\Html\Wrapper;
use Fox\Dao\SqlFragment;
use Hive\Ressource\Group;
use Fox\Generator;
use Fox\Html\Block;
use Fox\Html\Screen;
use Fox\Html\Input;

$xview = Xview::instance();

// Customizing
$customizing = Hive\Admin\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'users','act'=>'groups','sac'=>null));

if(!Fox\Authorization\Manager::check('hive-users-groups')) {
	echo $xview->accessDenied(Msg(Hive::BUNDLE,Hive::ERR_ADMIN_ACCES_DENIED),null,true);
	exit;
}
$reload = $xview->val('reload',false);

$screen = new Screen('sGroups');
$screen->pageTitle(Msg(Hive::BUNDLE,Hive::RESSOURCE_GROUPS));
$screen->message('<p class="prettyprint">'.Msg(Hive::BUNDLE,Hive::HELP_USER_GROUPS).'</p>');

$fragments = null;
$finder = new Group();
		$data = $finder->find(SqlFragment::combine($fragments),array('name' => 'ASC'));
$grid = new Fox\Grid('gGroups',Generator::url('users','groups'));
$grid->fieldcat(array('name','users','active','iedit'));
$grid->behaviors(Behaviors::value('bbg'));
$grid->fieldcat()->field('users')
		->headerAlign('center')
		->align('center')
		->label(Msg(Hive::BUNDLE,Hive::USER_GROUP_NB))
		->width(10);
$grid->bootstrapGrid();
$grid->fieldcat()->switchToObjectMode();
$grid->setCoche();
// Styling

// Data Generators
$grid->fieldcat()->setGenerators(array(
	'users' => Generator::useFunction(function ($data){
		foreach($data->users() as $user) {
			$detail[] = array('label' => \User::fullName($user->idusr),'idusr' => $user->idusr);
		}
		if(isset($detail)) {
			return $detail;
		}
		return null;
	}),
	'active' =>Generator::useFunction(function($data){
		return $data->active;
	}),
	'iedit' => '?',
	'coche' => Generator::useValue(false)
));
$grid->fieldcat()->field('coche')->noOutput();

$grid->fieldcat()->field('users')
	->width(20)
	->renderers(new Token(Generator::url('users','groups','unassign',array(
										'grouping' => Generator::useElement('grouping'),
										'idusr' => Generator::useElement('idusr')))))
	->formater(new Void());
	
$grid->fieldcat()->field('iedit')->renderer()
		->behaviors(Behaviors::action(
			Generator::url('users','groups','modify',array('grouping' => Generator::useAttribute())),
			true
		));
		
$grid->footer()->add(new Button('add','?',Msg(Hive::BUNDLE,Hive::USER_GROUP_ADD)))
		->color(Button::COLOR_WARNING)
		->behaviors(Behaviors::action(
			Generator::url('users','groups','modify'),
			true
		));
$grid->initialize($data);
if(!$screen->submitted() && !$reload) {
	$screen->add($grid);
	echo $screen->display();
} else {
	echo $grid->display();
}